﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using papercut2.Data.Global;

namespace papercut2.Data.ImageProcess
{
    //封装灰度化处理的方法
    class Gray
    {
        private static Bitmap pic = null;
        public static Bitmap Pic
        {
            get
            {
                return pic;
            }
            set
            {
                pic = value;
            }
        }
        public static Bitmap GrayPictrue()
        {
            int width = pic.Width;
            int height = pic.Height;
            Bitmap temp = new Bitmap(width, height);

            int r, g, b;
            int Result;
            Color pixel;
            for (int x = 0; x < width; x++)
                for (int y = 0; y < height; y++)
                {
                    pixel = pic.GetPixel(x, y);
                    r = pixel.R;
                    g = pixel.G;
                    b = pixel.B;
                    //以加权平均值法产生黑白图像
                    int iType = 0;//GlobalData.Choice;
                    switch (iType)
                    {
                        case 0://平均值法
                            Result = ((r + g + b) / 3);
                            temp.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
                            break;
                        case 1://最大值法
                            Result = r > g ? r : g;
                            Result = Result > b ? Result : b;
                            temp.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
                            break;
                        case 2://加权平均值法						 
                            Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));
                            temp.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
                            break;
                    }  
                }
            pic = temp;
            return temp;
        }
    }
}
